<?php

namespace app\admin\model;

use think\Model;


class Deviceorder extends Model
{

    

    

    // 表名
    protected $name = 'device_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text'
    ];
    

    
    public function getStatusList()
    {
        return [ 'payed' => __('已支付'), 'done' => __('出货成功'), 'fail' => __('出货失败'), 'refund' => __('已退款')];
    }

    public function getStatus($status)
    {
        if($status == 'paying'){

            return "未支付";
        }
        if($status == 'done'){

            return "出货成功";
        }
        if($status == 'fail'){

            return "出货失败";
        }
        if($status == 'payed'){

            return "已支付";
        }
        if($status == 'refund'){

            return "已退款";
        }
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function agent()
    {
        return $this->belongsTo('User', 'agent_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function device()
    {
        return $this->belongsTo('Device', 'device_id', 'device_id', [], 'LEFT')->setEagerlyType(0);
    }
}
